<?php
Class Controller_Manage_Export extends Controller_Abstract
{
	function actionList()
	{
		$this->_view['title'] = '信息导出列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = VegeSupplier::search($cp, $ps, $url_arr);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Pagec($result['pagination'], url('Manage::Export/List', $url_arr));
			$this->_view['page'] = $page->getPage();
            $this->_view['pagecount'] = $result['pagination'];
			$this->_view['list'] = $result['data'];
		}
	}
	
	function actionListTime()
	{
		$this->_view['title'] = '按时间信息导出列表';
		/**
		思路1：可以使用js,通过提交按钮获取到选择的时间，然后修改form的action或者直接使用submit()提交表单
		思路2：直接提交表单
		*/
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = VegeSupplier::search($cp, $ps, $url_arr);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Pagec($result['pagination'], url('Manage::Export/List', $url_arr));
			$this->_view['page'] = $page->getPage();
            $this->_view['pagecount'] = $result['pagination'];
			$this->_view['list'] = $result['data'];
		}
	}
	
	function actionIn()
	{
		$this->_view['title'] = '导出进场';
		//echo "导出进场";exit;
		@ini_set('output_buffering ', 'On');
		@ini_set('display_errors ', 0);
		ob_start(); //打开缓冲区
		$begin = isset($_POST['inbegintime'])?$_POST['inbegintime']:'';
		$end = isset($_POST['inendtime'])?$_POST['inendtime']:'';
		if(!trim($begin))
		{
			$begin = date('Y-m-d');
		}
		
		if(!trim($end))
		{
			$end = date('Y-m-d');
		}
		
		$date = OLM_Normal::getMonthNum($begin,$end);
		if($date>6)
		{
			return $this->_redirectMessage('导出失败', '输入时间不能大于半年！', url('Manage::Export/List'), FAILURE);
		}
		
		$where = '1';
		$where .= ' and time>=\''.$begin.'\' and time<=\''.$end.' 23:59:59\'';
		
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$data = VegePfIn::find($where)->asArray()->getAll();
		//dump($data);exit;
		if ($data)
		{
			$baseinfo = BaseNodeInfo::find()->asArray()->getOne();
			
			$filename = $baseinfo['companynumber'] .'_'. $begin . '_PC1.txt'; //设置文件名
			header('Content-Type: text/txt');
			header("Content-Disposition: attachment;filename={$filename}");
			$out = fopen('php://output', 'w');
			foreach($data as $d)
			{
				$line = '';
				$line .= ",".mb_convert_encoding($d['pfmarketnumber'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['pfmarketname'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding(str_replace('-','',$d['intime']), "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['pfnumber'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['pfname'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding("9998", "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['batch'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['productno'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['productname'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['inquantity'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['inweight'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['madeinno'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['madeinname'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding("", "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['suppliername'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding("", "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['cardno'], "gb2312", "utf-8");
				fputs($out, $line."\r\n");
			}
			
			fclose($out);
		}
		else
		{
			return $this->_redirectMessage('导出失败', '没有任何信息', url('Manage::Export/List'), FAILURE);
		}
	}
	
	function actionInHz()
	{
		$this->_view['title'] = '导出进场汇总';
		//echo "导出进场汇总";exit;
		@ini_set('output_buffering ', 'On');
		@ini_set('display_errors ', 0);
		ob_start(); //打开缓冲区
		$begin = isset($_POST['inhzbegintime'])?$_POST['inhzbegintime']:'';
		$end = isset($_POST['inhzendtime'])?$_POST['inhzendtime']:'';
		if(!trim($begin))
		{
			$begin = date('Y-m-d');
		}
		
		if(!trim($end))
		{
			$end = date('Y-m-d');
		}
		
		$date = OLM_Normal::getMonthNum($begin,$end);
		if($date>6)
		{
			return $this->_redirectMessage('导出失败', '输入时间不能大于半年！', url('Manage::Export/List'), FAILURE);
		}
		
		$where = '1';
		$where .= ' and time>=\''.$begin.'\' and time<=\''.$end.' 23:59:59\'';
		
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$data = VegePfInHz::find($where)->asArray()->getAll();
		//dump($data);exit;
		if ($data)
		{
			$baseinfo = BaseNodeInfo::find()->asArray()->getOne();
			
			$filename = $baseinfo['companynumber'] .'_'. $begin . '_PCHZ1.txt'; //设置文件名
			header('Content-Type: text/txt');
			header("Content-Disposition: attachment;filename={$filename}");
			$out = fopen('php://output', 'w');
			foreach($data as $d)
			{
				$line = '';
				$line .= ",".mb_convert_encoding($d['pfmarketnumber'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding(str_replace('-','',$d['indate']), "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['productno'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['inquantity'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['inweight'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['madeinno'], "gb2312", "utf-8");
				fputs($out, $line."\r\n");
			}
			
			fclose($out);
		}
		else
		{
			return $this->_redirectMessage('导出失败', '没有任何信息', url('Manage::Export/List'), FAILURE);
		}
	}
	
	function actionCheck()
	{
		$this->_view['title'] = '导出检测';
		//echo "导出检测";exit;
		@ini_set('output_buffering ', 'On');
		@ini_set('display_errors ', 0);
		ob_start(); //打开缓冲区
		set_time_limit(0);
		$begin = isset($_POST['ckbegintime'])?$_POST['ckbegintime']:'';
		$end = isset($_POST['ckendtime'])?$_POST['ckendtime']:'';
		if(!trim($begin))
		{
			$begin = date('Y-m-d');
		}
		
		if(!trim($end))
		{
			$end = date('Y-m-d');
		}
		
		$date = OLM_Normal::getMonthNum($begin,$end);
		if($date>6)
		{
			return $this->_redirectMessage('导出失败', '输入时间不能大于半年！', url('Manage::Export/List'), FAILURE);
		}
		
		$where = '1';
		$where .= ' and time>=\''.$begin.'\' and time<=\''.$end.' 23:59:59\'';
		
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$data = VegePfCheck::find($where)->asArray()->getAll();
		//dump($data);exit;
		if ($data)
		{
			$baseinfo = BaseNodeInfo::find()->asArray()->getOne();
			
			$filename = $baseinfo['companynumber'] .'_'. $begin . '_PC3.txt'; //设置文件名
			header('Content-Type: text/txt');
			header("Content-Disposition: attachment;filename={$filename}");
			$out = fopen('php://output', 'w');
			foreach($data as $d)
			{
				$line = '';
				$line .= ",".mb_convert_encoding($d['pfmarketnumber'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['pfmarketname'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding(str_replace('-','',$d['checkdate']), "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['pfnumber'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['pfname'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['batch'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['productno'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['productname'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['sampleno'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['detector'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['checkresult'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['checkinformation'], "gb2312", "utf-8");
				fputs($out, $line."\r\n");
			}
			
			fclose($out);
		}
		else
		{
			return $this->_redirectMessage('导出失败', '没有任何信息', url('Manage::Export/List'), FAILURE);
		}
	}
	
	function actionOut()
	{
		$this->_view['title'] = '导出出场';
		//echo "导出出场";exit;
		@ini_set('output_buffering ', 'On');
		@ini_set('display_errors ', 0);
		ob_start(); //打开缓冲区
		set_time_limit(0);
		$begin = isset($_POST['outbegintime'])?$_POST['outbegintime']:'';
		$end = isset($_POST['outendtime'])?$_POST['outendtime']:'';
		if(!trim($begin))
		{
			$begin = date('Y-m-d');
		}
		
		if(!trim($end))
		{
			$end = date('Y-m-d');
		}
		
		//$date=floor((strtotime($enddate)-strtotime($startdate))/86400);//相差的天数
		$date = OLM_Normal::getMonthNum($begin,$end);
		if($date>6)
		{
			return $this->_redirectMessage('导出失败', '输入时间不能大于半年！', url('Manage::Export/List'), FAILURE);
		}
		
		$where = '1';
		$where .= ' and time>=\''.$begin.'\' and time<=\''.$end.' 23:59:59\'';
		
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$data = VegePfOut::find($where)->asArray()->getAll();
		//dump($data);exit;
		if ($data)
		{
			$baseinfo = BaseNodeInfo::find()->asArray()->getOne();
			
			$filename = $baseinfo['companynumber'] .'_'. $begin . '_PC2.txt'; //设置文件名
			header('Content-Type: text/txt');
			header("Content-Disposition: attachment;filename={$filename}");
			$out = fopen('php://output', 'w');
			foreach($data as $d)
			{
				//mb_convert_encoding($d['pfmarketnumber'], "gb2312", "utf-8");
				$line = '';
				$line .= ",".mb_convert_encoding($d['pfmarketnumber'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['pfmarketname'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding(str_replace('-','',$d['outtime']), "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['pfnumber'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['pfname'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['retailnumber'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['retailname'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['code'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['productno'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['productname'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['outquantity'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['outweight'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['outprice'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['outamount'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['address'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['batch'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['incode'], "gb2312", "utf-8");
				/*$line .= ",".$d['pfmarketnumber'];
				$line .= ",".$d['pfmarketname'];
				$line .= ",".str_replace('-','',$d['outtime']);
				$line .= ",".$d['pfnumber'];
				$line .= ",".$d['pfname'];
				$line .= ",".$d['retailnumber'];
				$line .= ",".$d['retailname'];
				$line .= ",".$d['code'];
				$line .= ",".$d['productno'];
				$line .= ",".$d['productname'];
				$line .= ",".$d['outquantity'];
				$line .= ",".$d['outweight'];
				$line .= ",".$d['outprice'];
				$line .= ",".$d['outamount'];
				$line .= ",".$d['address'];
				$line .= ",".$d['batch'];
				$line .= ",".$d['incode'];*/
				fputs($out, $line."\r\n");
			}
			
			fclose($out);
			//ob_end_clean();//应该使用clean
		}
		else
		{
			return $this->_redirectMessage('导出失败', '没有任何信息', url('Manage::Export/List'), FAILURE);
		}
	}
	
	function actionOutHz()
	{
		$this->_view['title'] = '导出出场汇总';
		//echo "导出出场汇总";exit;
		@ini_set('output_buffering ', 'On');
		@ini_set('display_errors ', 0);
		ob_start(); //打开缓冲区
		set_time_limit(0);
		$begin = isset($_POST['outhzbegintime'])?$_POST['outhzbegintime']:'';
		$end = isset($_POST['outhzendtime'])?$_POST['outhzendtime']:'';
		if(!trim($begin))
		{
			$begin = date('Y-m-d');
		}
		
		if(!trim($end))
		{
			$end = date('Y-m-d');
		}
		
		$date = OLM_Normal::getMonthNum($begin,$end);
		if($date>6)
		{
			return $this->_redirectMessage('导出失败', '输入时间不能大于半年！', url('Manage::Export/List'), FAILURE);
		}
		
		$where = '1';
		$where .= ' and time>=\''.$begin.'\' and time<=\''.$end.' 23:59:59\'';
		
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$data = VegePfOutHz::find($where)->asArray()->getAll();
		//dump($data);exit;
		if ($data)
		{
			$baseinfo = BaseNodeInfo::find()->asArray()->getOne();
			
			$filename = $baseinfo['companynumber'] .'_'. $begin . '_PCHZ2.txt'; //设置文件名
			header('Content-Type: text/txt');
			header("Content-Disposition: attachment;filename={$filename}");
			$out = fopen('php://output', 'w');
			foreach($data as $d)
			{
				$line = '';
				$line .= ",".mb_convert_encoding($d['pfmarketnumber'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding(str_replace('-','',$d['outdate']), "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['productno'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['outquantity'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['outweight'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['outprice'], "gb2312", "utf-8");
				$line .= ",".mb_convert_encoding($d['outamount'], "gb2312", "utf-8");
				fputs($out, $line."\r\n");
			}
			
			fclose($out);
		}
		else
		{
			return $this->_redirectMessage('导出失败', '没有任何信息', url('Manage::Export/List'), FAILURE);
		}
	}
	
	function actionExportBusinessBaseInfo()
	{
		$this->_view['title'] = '导出供应商信息';
		set_time_limit(0);
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$vegesupplier = VegeSupplier::find()->asArray()->getAll();
		//dump($businessbaseinfo);exit;
		if ($vegesupplier)
		{
			$baseinfo = BaseNodeInfo::find()->asArray()->getOne();
			
			$filename = $baseinfo['companynumber'] . '_KHDA.txt'; //设置文件名
			header('Content-Type: text/txt');
			header("Content-Disposition: attachment;filename={$filename}");
			$out = fopen('php://output', 'w');
			foreach($vegesupplier as $vs)
			{
				$line .= ",".$vs['suppliernumber'];
				$line .= ",".$vs['suppliername'];
				$line .= ",".$vs['regnum'];
				$line .= ",".str_replace('-','',$vs['recorddate']);
				$line .= ",".$vs['corporate'];
				$line .= ",".$vs['phone'];
				$line .= ",".str_replace('-','',$vs['updatetime']);
				fputs($out, $line."\r\n");
			}
			
			fclose($out);
		}
		else
		{
			return $this->_redirectMessage('导出失败', '没有任何信息', url('Manage::Export/List'), FAILURE);
		}
	}
}